<?php
	require("frmCookie.php");
	
	$iOpcion = $_GET[iOpcion];
	$iMensaje = $_GET[iMensaje];
	
	switch ($iMensaje)
	{
		case 1:
		{
			$mensaje = "El movimiento se registro con exito.";
			break;
		}
	}
	
	switch($iOpcion)
	{
		case 3:
		{
			$mySQLRemito = "SELECT * FROM stockentransito WHERE nroRemito='" . $_POST[txtRemitoDef] . "'";
			$resultRemito = readSQL($mySQLRemito);
			
            foreach($resultRemito as $valueRemito)
            {
            	$mySQLStock = "INSERT INTO stock(idParte, idAlmacen, idMoneda, imei, nroFactura, codigoReferencia, swVersion, hwVersion, precio, cantidad, fecha, idOrdenCompra, idProveedor, nroRemitoLlegada, simlock) VALUES(";            	
    			$mySQLStock = $mySQLStock . $valueRemito['idParte'] . ", ";
    			$mySQLStock = $mySQLStock . $valueRemito['idAlmacen'] . ", ";
    			$mySQLStock = $mySQLStock . $valueRemito['idMoneda'] . ", ";
    			$mySQLStock = $mySQLStock . "'" . $valueRemito['imei'] . "', ";
    			$mySQLStock = $mySQLStock . "'" . $valueRemito['nroFactura'] . "', ";
    			$mySQLStock = $mySQLStock . "'" . $valueRemito['codigoReferencia'] . "', ";
    			$mySQLStock = $mySQLStock . "'" . $valueRemito['swVersion'] . "', ";
    			$mySQLStock = $mySQLStock . "'" . $valueRemito['hwVersion'] . "', ";
    			$mySQLStock = $mySQLStock . $valueRemito['precio'] . ", ";
    			$mySQLStock = $mySQLStock . $valueRemito['cantidad'] . ", ";
    			$mySQLStock = $mySQLStock . "'" . $valueRemito['fecha'] . "', ";
				$mySQLStock = $mySQLStock . "'" . $valueRemito['idOrdenCompra'] . "', ";
				$mySQLStock = $mySQLStock . "'" . $valueRemito['idProveedor'] . "', ";
				$mySQLStock = $mySQLStock . "'" . $valueRemito['nroRemitoLlegada'] . "', ";
				$mySQLStock = $mySQLStock . "'" . $valueRemito['simlock'] . "')";
            	executeSQL($mySQLStock);
            	//echo $mySQLStock . "<br>";

            	$mySQLMax = "SELECT MAX(idStock) FROM stock";
				$resultMax = readSQL($mySQLMax);
				foreach($resultMax as $valueMax)
				{
					$idStock = $valueMax[0];
				}
    			
    			$mySQLUsos = "SELECT * FROM tiposusosxstockentransito WHERE idStockEnTransito=" . $valueRemito['idStockEnTransito'];
        		$resultUsos = readSQL($mySQLUsos);
        		foreach($resultUsos as $valueUsos)
        		{
        			$mySQLUsosTran = "INSERT INTO tiposusosxstock VALUES(";
        			$mySQLUsosTran = $mySQLUsosTran . $valueUsos['idTipoUso'] . ", ";
        			$mySQLUsosTran = $mySQLUsosTran . $valueUsos['idParte'] . ", ";
        			$mySQLUsosTran = $mySQLUsosTran . "'" . $valueUsos['imei'] . "', ";
        			$mySQLUsosTran = $mySQLUsosTran . "'" . $valueUsos['nroFactura'] . "', ";
        			$mySQLUsosTran = $mySQLUsosTran . $idStock . ")";
        			executeSQL($mySQLUsosTran);
        			//echo $mySQLUsosTran . "<br>";
        		}
            }
            $mySQLDelete = "DELETE FROM stockentransito WHERE nroRemito='" . $_POST[txtRemitoDef] . "'";
            executeSQL($mySQLDelete);
            //echo $mySQLDelete . "<br>";
            
            header("Location: movimientos.php?iOpcion=1&iMensaje=1");
			break;
		}
	}
?>
<html>
	<head>
    	<?php
		require("frmHeader.php");
		?>
		<script language="javascript" src="scripts/ajax.js"></script>
		<script language="javascript" src="scripts/validar.js"></script>
		<script language="javascript">
			
			function showRemito(str)
			{
				xmlHttp = GetXmlHttpObject();
				if (xmlHttp == null)
	  			{
	  				alert ("Your browser does not support AJAX!");
	  				return;
	  			}
	  			 
				var url = "ajax.php";
				url = url + "?id=" + str;
				url = url + "&op=41";
				url = url + "&q=" + frm.txtNroRemito.value;
				url = url + "&sid=" + Math.random();
				xmlHttp.onreadystatechange = stateChangedRemito;
				xmlHttp.open("GET",url,true);
				xmlHttp.send(null);
			}
	
			function stateChangedRemito() 
			{ 
				if (xmlHttp.readyState == 4)
				{ 
					if(xmlHttp.responseText != "")
					{
						document.frm.submit();
					}
					else
					{
						document.getElementById("txtHint").innerHTML = "Remito Inexistente.";
					}
				}
			}
			
			function valida(inOp)
			{
				switch(inOp)
				{
					case 1:
					{
						if(frm.txtNroRemito.value == "")
						{
							alert("Por favor ingrese el nro de remito.");
							frm.txtNroRemito.focus();
							return false;
						}
						
						showRemito('<?php echo $_SESSION['idSucursal']; ?>');
						
						return false;
						
						break;
					}
					case 2:
					{
						document.frm.submit();
						
						break;
					}
				}
			}
		</script>
	</head>
	<body>
    	<table cellpadding="0" cellspacing="0" align="center" class="tablaPrincipal">
        	<tr>
            	<td align="center">
                	<?php
					require("frmSession.php"); 
					
					//Redirecciona a SINPERMISOS.PHP en caso de no tener permisos
					switch ($iOpcion)
					{
						case 1:
						case 2:
						{
							
							if (!(in_array(13002, $_SESSION[permisos])))
							{
							?>
							  <script language="JavaScript" type="text/javascript">
							  <!--
							  window.location.href = "sinPermisos.php"
							  //-->
							  </script>
							<?php
							}
							$titulo = "Recepcion de Stock";
							break;
						}
						case 3:
						case 4:
						{
							if (!in_array(13002, $_SESSION[permisos]))
							{
							?>
							  <script language="JavaScript" type="text/javascript">
							  <!--
							  window.location.href = "sinPermisos.php"
							  //-->
							  </script>
							<?php
							}
							$titulo = "Recepcion de Stock";
							break;
						}
						case 5:
						case 6:
						case 7:
						{
							if (!in_array(13002, $_SESSION[permisos]))
							{
							?>
							  <script language="JavaScript" type="text/javascript">
							  <!--
							  window.location.href = "sinPermisos.php"
							  //-->
							  </script>
							<?php
							}
							$titulo = "Recepcion de Stock";
							break;
						}
					}
					
					?>
                </td>
            </tr>
            <tr>
            	<td align="center">
                	<?php
					require("frmTitulo.php");
					?>
                </td>
            </tr>
            <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
            	<td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="menuPrincipal">
                    	<tr>
                        	<td>
								<?php
                                require("frmMenu.php");
                                ?>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
                <td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="tituloPantalla">
                    	<tr>
                        	<td align="center">
                            	<p class="texto" align="left"><?php echo $titulo; ?></p>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
             <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
                <td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="estadoPantalla">
                    	<tr>
                        	<td align="center">
                            	<p class="texto" align="left"><?php echo $mensaje; ?></p>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
             <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
                <td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="contenedorPrincipal">
                    	<tr>
                        	<td align="">
                        		<?php
                				switch($iOpcion)
                				{
                					case 1:
                					{
        						?>
        						<form name="frm" action="recepcion.php?iOpcion=2" method="post" onsubmit="return valida(1);">
        							<table width="400">
        						<?php
                						break;
                					}
                					case 2:
                					{
            					?>
            					<form name="frm" action="recepcion.php?iOpcion=3" method="post" onSubmit="return valida(2);">
            						<table>
            					<?php	
                						break;
                					}
                				}
        						?>
		                				<?php
		                				switch($iOpcion)
		                				{
		                					case 1:
		                					{
                						?>
		                				<tr>
		                					<td width="100">Numero de Remito:</td>
		                					<td><input type="text" name="txtNroRemito" size="20" maxlength="20" /></td><td><div id="txtHint"></div></td>
		                				</tr>
		                				<tr>
		                					<td colspan="2" align="center"><input type="submit" name="cmdEnviar" value="Continuar"></td>
		                				</tr>
                						<?php
		                						break;
		                					}
		                					case 2:
		                					{
            							?>
            							<tr>
            								<td colspan="3">Numero de Remito: <b><?php echo $_POST[txtNroRemito]; ?></b></td>
            							</tr>
            							<tr>
            								<td>&nbsp;</td>
            							</tr>
        								<tr>
        									<th class="listaHead">Codigo Tango</th>
        									<th class="listaHead">Parte</th>
        									<th class="listaHead">Cantidad Transferida</th>
        								</tr>
        								<?php
    									$mySQL = "SELECT P.idParte, P.descripcionFabricante, P.codigoTango, S.cantidad FROM stockentransito S, partes P WHERE S.idParte = P.idParte AND S.nroRemito='" . $_POST[txtNroRemito] . "'";
    									$result = readSQL($mySQL);
			    							
			    						$i = 0;	
		    							
		  				                foreach($result as $value)
		  				                {
	  					                	echo "<tr>";
	  					                	if($i%2==0)
											{
                                        	?>
                                        	<td class="par"><li><?php echo $value['codigoTango']; ?></li></td>
                                        	<td class="par"><li><?php echo $value['descripcionFabricante']; ?></li></td>
                                        	<td class="par" align="right"><input type="text" name="txt<?php echo $value['idParte']; ?>desde" value="<?php echo $value['cantidad']; ?>" style="border:none;text-align:right;background:transparent;" size="2" align="right" readonly></td>
                                        	<?php
											}
											else
											{
											?>
											<td class="par"><li><?php echo $value['codigoTango']; ?></li></td>
											<td class="impar"><li><?php echo $value['descripcionFabricante']; ?></li></td>
                                        	<td class="impar" align="right"><input type="text" name="txt<?php echo $value['idParte']; ?>desde" value="<?php echo $value['cantidad']; ?>" style="border:none;text-align:right;background:transparent;" size="2" align="right" readonly></td>
											<?php
											}
											$i++;
	  					                	echo "</tr>";
		  				                }
        								?>
        								<tr>
        									<td colspan="3" align="center"><input type="submit" name="cmdEnviar" value="Recibir"></td>
        								</tr>
		                				<input type="hidden" name="txtRemitoDef" value="<?php echo $_POST[txtNroRemito]; ?>"/>
            							<?php
	                							break;
		                					}
		                				}
		                				?>
		                			</table>
        		            	</form>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
	</body>
</html>